package logic

import (
	"context"
	"gorm.io/gorm"
	"shuqi-novel/models/mysql"

	"shuqi-novel/novelrpc/internal/svc"
	"shuqi-novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelUpdateLogic {
	return &NovelUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelUpdateLogic) NovelUpdate(in *novelrpc.NovelUpdateRequest) (*novelrpc.NovelUpdateResponse, error) {
	// todo: add your logic here and delete this line

	novel := &mysql.Novel{
		Model:     gorm.Model{ID: uint(in.NovelId)},
		Title:     in.Title,
		Desc:      in.Desc,
		NovelType: in.NovelType,
		UserId:    in.UserId,
	}

	if err := novel.NovelUpdate(mysql.DB, in.NovelId); err != nil {
		return nil, err
	}

	return &novelrpc.NovelUpdateResponse{
		NovelId: in.NovelId,
	}, nil
}
